package com.example.jdbc.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.jdbc.bean.LearnResource;
import com.example.jdbc.service.UserService;

@RestController
public class UserController {
	Logger logger = LoggerFactory.getLogger(UserController.class);

	@Resource
	private UserService UserService;

	@RequestMapping("/find")
	public List<LearnResource> findAll() {
		return UserService.findAll();
	}

	@RequestMapping("/delete")
	public void testdelete(){
		int i=UserService.delete(11);
		if(i!=0){
			logger.info("删除成功");
		}
	}
	@RequestMapping("/save")
	public void testsave() {
		LearnResource use = new LearnResource();
		use.setUrl("http://www.cnblogs.com/tomlxq/p/5514658.html");
		use.setTitle("springboot 链接数据库");
		use.setAuthor("嘟嘟");
		use.setId((int)Math.random()*128);
		UserService.testsave(use);
	}

	@RequestMapping("/update")
	public void updateById(HttpServletRequest request, HttpServletResponse response) {
		LearnResource use = new LearnResource();
//		use.setId(Integer.valueOf(request.getParameter("id")));
		use.setId(1);
		LearnResource title = UserService.findOne(use.getId());
		if (title == null) {
			logger.info("没有该用户！！！！");
		} else {
			use.setTitle("美丽");
			use.setAuthor("赵小静");
			use.setUrl("https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=jdbcTemplate+++%E6%9F%A5%E8%AF%A2sql%E6%80%8E%E4%B9%88%E5%86%99");
			int updateUser = UserService.updateById(use);
			if (updateUser > 0) {
				logger.info("更改成功！");
			}
		}
	}
}
